package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.pojo.SkuImage;
import com.atguigu.gmall.product.pojo.SkuInfo;
import com.atguigu.gmall.product.pojo.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/rpc/product")
public class SkuRpcController {
    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SkuImageService skuImageService;

    /**
     * 获取SkuInfo的基本属性
     * @param skuId
     * @return
     */
    @GetMapping("/skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 获取Sku照片
     * @param skuId
     * @return
     */
    @GetMapping("/skuImages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable Long skuId){
        List<SkuImage> skuImages = skuImageService.list(new LambdaQueryWrapper<SkuImage>().eq(SkuImage::getSkuId, skuId));
        return Result.ok(skuImages);
    }

    /**
     * 获取Sku的实时价格。
     * @param skuId
     * @return
     */
    @GetMapping("/skuPrice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable Long skuId){
        SkuInfo skuInfo = skuInfoService.lambdaQuery().select(SkuInfo::getPrice).eq(SkuInfo::getId, skuId).one();
        return Result.ok(skuInfo.getPrice());
    }

    @GetMapping("/spuSaleAttr/{spuId}/{skuId}")
    public  Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable Long spuId, @PathVariable Long skuId){
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.getSpuSaleAttr(spuId,skuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 获取ValueJsong
     * @param skuId
     * @return
     */
    @GetMapping("/valuesSkuJson/{spuId}")
    public Result<String>   getValuesSpuJson(@PathVariable Long  spuId){
        String json = spuSaleAttrService.getValuesSpuJson(spuId);
        return Result.ok(json);
    }

}
